using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using EnvDTE;

namespace Skim.Ssms.AddIn.ActiveFileExplorer.Loader
{
    /// <summary>
    /// Default document loader that displays active files by names without any grouping
    /// </summary>
    public class NameDocumentLoader : DocumentLoader
    {
        protected override void LoadTreeView(TreeView treeView, IEnumerable<Document> documents)
        {
            Func<Document, IDocumentInfo> getInfo = DocumentInfoFactory.GetInfo;
            Func<Document, string> getDocumentName = document => getInfo(document).GetDocumentName();
            Func<Document, int> getImageIndex = document => GetImageIndex(getInfo(document).DocumentImageIndex);

            var sortedDocuments = from document in DocumentTypeFilter.FilterOpenDocuments(documents)
                                  orderby getDocumentName(document)
                                  select document;

            foreach (var document in sortedDocuments)
            {
                var node = treeView.Nodes.Add(getDocumentName(document));
                node.ImageIndex = getImageIndex(document);
                node.Tag = document;
            }
        }
    }
}